/*
 * @Description: 基础方法
 * @Author: Haor
 * @Date: 2022-04-01 16:50:13
 * @LastEditTime: 2022-05-08 23:17:42
 * @LastEditors: Haor
 */

/**
 * 是否为空
 * @param {*} data
 * @return {Boolean}
 */
export const isEmpty = (data: any): boolean => {
	// base type
	if ([undefined, null, '', 0, NaN, false].includes(data)) return true
	if (data instanceof Error) return data.message === ''

	// reference type
	switch (Object.prototype.toString.call(data)) {
		case '[object String]':
		case '[object Array]':
			return !data.length
		case '[object File]':
		case '[object Map]':
		case '[object Set]':
			return !data.size
		case '[object Object]':
			return !Object.keys(data).length
		default:
			return false
	}
}

/**
 * and条件
 * @return {Boolean}
 */
export const and = function (...args: any[]): boolean {
	return args.every(item => {
		return (typeof item === 'function') ? item() : !!item
	})
}

/**
 * 生成指定长度数字
 * @param {Number} len
 * @return {String}
 */
export const randomNumber = function(len: number): string {
    let count = ''
    while(count.length < len) {
        count += (Math.floor(Math.random() * 10)).toString()
    }
    return count
}
